package com.kuang.servlet.bill;

import com.kuang.pojo.Bill;
import com.kuang.pojo.Provider;
import com.kuang.service.bill.BillService;
import com.kuang.service.bill.BillServiceImpl;
import com.kuang.service.provider.ProviderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class BillServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if (method.equals("query")&&method!=null){
            this.query(req,resp);
        }
        if (method.equals("add")&&method!=null){
            this.add(req,resp);
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    //增加订单
    public void add(HttpServletRequest req, HttpServletResponse resp){
        ProviderServiceImpl providerService = new ProviderServiceImpl();
        List<Provider> providerList = new ArrayList<Provider>();
        providerList = providerService.getProviderList();
        req.setAttribute("providerId",providerList);

        String billCode = req.getParameter("billCode");
        String productName = req.getParameter("productName");
        String productUnit = req.getParameter("productUnit");
        String productCount = req.getParameter("productCount");
        String totalPrice = req.getParameter("totalPrice");
        String providerId = req.getParameter("providerId");

        Bill bill = new Bill();
        bill.setBillCode(billCode);
        bill.setProductName(productName);
        bill.setProductUnit(productUnit);
        bill.setProductCount(new BigDecimal(productCount).setScale(2,BigDecimal.ROUND_DOWN));
        bill.setTotalPrice(new BigDecimal(totalPrice).setScale(2,BigDecimal.ROUND_DOWN));
        bill.setProviderId(Integer.valueOf(providerId));
        boolean flag = false;
        BillService billService = new BillServiceImpl();
        flag = billService.add(bill);
        if (flag){
            try {
                resp.sendRedirect("/smbms/jsp/bill.do?method=query");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else {
            try {
                req.getRequestDispatcher("billadd.jsp").forward(req,resp);
            } catch (ServletException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }


    //查询商品列表
    //从前端获取数据
    public void query(HttpServletRequest req, HttpServletResponse resp){
        String queryProductName = req.getParameter("queryProductName");
        String queryIsPayment = req.getParameter("queryIsPayment");
        String temp = req.getParameter("queryProviderId");//临时的
        int queryProviderId = 0;
        int queryIsPayment1 = 0;
        //获取商品列表
        BillServiceImpl billService = new BillServiceImpl();
        List<Bill> billList = null;

        if (queryProductName == null){
            queryProductName = "";
        }
        if (queryIsPayment != null && !queryIsPayment.equals("")){
            queryIsPayment1 = Integer.parseInt(queryIsPayment);
        }
        if (temp != null && !temp.equals("")){
            queryProviderId = Integer.parseInt(temp);
        }

        billList = billService.getBillList(queryProductName,queryProviderId,queryIsPayment1);
        req.setAttribute("billList",billList);

        ProviderServiceImpl providerService = new ProviderServiceImpl();
        List<Provider> providerList = providerService.getProviderList();
        req.setAttribute("providerList",providerList);

        req.setAttribute("queryProductName",queryProductName);

        try {
            req.getRequestDispatcher("billlist.jsp").forward(req,resp);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
